package com.wudl.java8.stream;

import com.sun.corba.se.pept.transport.Connection;
import com.wudl.java8.bean.User;

import java.util.*;
import java.util.function.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName : UserStream
 * @Description : Stream  的使用
 * @Author :wudl
 * @Date: 2021-08-17 22:59
 */

public class UseStream {

    public static void main(String[] args) {
        //
//        UseStream.StreamToSet();

//        StreamToMap();
//        StreamToReduceToSum();

//        StreamMaxAndMin();
//        StreamMatch();
//        MyStreamFor();
//        StreamFilter();
//        StreamTOSort();

        StreamLimitAndSkip();
    }





    /**
     * 流的创建
     *
     * @return
     */
    public static Stream<User> CreateStream() {
        List<User> userEntities = new ArrayList<>();
        userEntities.add(new User("mayikt", 20));
        userEntities.add(new User("meite", 28));
        userEntities.add(new User("zhangsan", 35));
        userEntities.add(new User("xiaowei", 16));
        userEntities.add(new User("Flink", 22));
        userEntities.add(new User("hdfs", 18));

        userEntities.stream();
        Stream<User> userStream = userEntities.parallelStream();
        return userStream;
    }

    /**
     * 将lIST 转为流 在通过流转化为Set 集合
     */
    public static void StreamToSet() {
        Stream<User> userStream = UseStream.CreateStream();
        Set<User> userSet = userStream.collect(Collectors.toSet());
        System.out.println("----------------StreamToSet---------------");
        System.out.println(userSet);
    }

    /**
     * 1.将list 转为流， 在通过流转化为map 集合
     */
    public static void StreamToMap() {
        Stream<User> userStream = UseStream.CreateStream();
        Map<String, User> userMap = userStream.collect(Collectors.toMap(new Function<User, String>() {
                   @Override
                   public String apply(User userEntity) {
                       return userEntity.getName();
                   }
               }, new Function<User, User>() {

                   @Override
                   public User apply(User User) {
                       return User;
                   }
               }
        ));
        userMap.forEach(new BiConsumer<String, User>() {
            @Override
            public void accept(String s, User user) {
                System.out.println(s + "====" + user.toString());
            }
        });
    }

    /**
     * 2.通过流就和 resuce
     */
    public static void StreamToReduceToSum() {
        Stream<Integer> stream = Stream.of(1, 2, 5, 6, 8);
        // 第一种求和实现
//        Optional<Integer> reduce = stream.reduce((a1, a2) -> a1 + a2);
        // 第二种求和实现
        Optional<Integer> reduce = stream.reduce(new BinaryOperator<Integer>() {
            @Override
            public Integer apply(Integer o, Integer o2) {
                return o + o2;
            }
        });
        System.out.println(reduce);
    }

    /**
     * 3.通过流求出两个流的最大值和最小值  max  和 min
     */
    public static void StreamMaxAndMin() {
        // 求最小值
        Stream<User> userStream = UseStream.CreateStream();
        Optional<User> max = userStream.max((a1, a2) -> {
            return a1.getAge() - a2.getAge();
        });
        System.out.println("max---" + max);

        // 求最小值
        Stream<User> userStreamMin = UseStream.CreateStream();
        Optional<User> min = userStreamMin.min((a1, a2) -> {
            return a1.getAge() - a2.getAge();
        });
        System.out.println("min--" + min);
    }

    /**
     * 4. 根据流匹配一个值 返回  true or false
     *      anyMatch表示，判断的条件里，任意一个元素成功，返回true
     *      allMatch表示，判断条件里的元素，所有的都是，返回true
     *      noneMatch跟allMatch相反，判断条件里的元素，所有的都不是，返回true
     */
    public static void StreamMatch() {
        Stream<User> userStream = UseStream.CreateStream();
        boolean b = userStream.noneMatch(new Predicate<User>() {
            @Override
            public boolean test(User user) {
                return user.getAge() > 50;
            }
        });

        //  5. allMatch  全匹配
        Stream<User> userStream2 = UseStream.CreateStream();
        boolean flink = userStream2.allMatch(new Predicate<User>() {
            @Override
            public boolean test(User user) {
                return user.getAge()<20;
            }
        });
        System.out.println("allMatch--"+flink);

        System.out.println(b);
    }

    /**
     *  6. 流的循环
     */
    public static void MyStreamFor()
    {
        Stream<User> userStream = UseStream.CreateStream();
        userStream.forEach(new Consumer<User>() {
            @Override
            public void accept(User user) {
                System.out.println(user.getAge() +"--"+user.getName());
            }
        });

        Stream<User> userStreamForeach = UseStream.CreateStream();
        userStreamForeach.forEach((user ->{
            System.out.println("2--"+user.getAge() +"--"+user.getName());
        }));

    }

    /**
     *  7.Stream过滤器
     */
    public  static void StreamFilter()
    {
        Stream<User> userStream = UseStream.CreateStream();
        Stream<User> flink = userStream.filter(new Predicate<User>() {
            @Override
            public boolean test(User user) {
                return user.getAge() > 20;
            }
        }).filter(new Predicate<User>() {
            @Override
            public boolean test(User user) {
                return user.getName().equals("Flink");
            }
        });
        flink.forEach(user ->{
            System.out.println(user.toString());
        });
    }

    /**
     *  8.流的排序
     */

    public static  void StreamTOSort()
    {
        List<User> userEntities = new ArrayList<>();
        userEntities.add(new User("mayikt", 20));
        userEntities.add(new User("meite", 28));
        userEntities.add(new User("zhangsan", 35));
        userEntities.add(new User("xiaowei", 16));
        userEntities.add(new User("Flink", 22));
        userEntities.add(new User("hdfs", 18));
        List<User> collect = userEntities.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList());
        collect.forEach(new Consumer<User>() {
            @Override
            public void accept(User user) {
                System.out.println(user.toString());
            }
        });
    }

    /**
     *  9. Stream Skip  和Limit
     */
    public static void StreamLimitAndSkip()
    {
        List<User> userEntities = new ArrayList<>();
        userEntities.add(new User("mayikt", 20));
        userEntities.add(new User("meite", 28));
        userEntities.add(new User("zhangsan", 35));
        userEntities.add(new User("xiaowei", 16));
        userEntities.add(new User("Flink", 22));
        userEntities.add(new User("hdfs", 18));

        Stream<User> stream = userEntities.stream();
        stream.skip(2).limit(3).forEach(u ->{
            System.out.println(u.toString());
        });

    }

}
